home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / libtiff / mkg3stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-27  |  23.9 KB  |  756 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/mkg3states.c,v 1.6 91/09/27 09:16:20 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991 Sam Leffler
  7.  * Copyright (c) 1991 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * Program to construct Group 3 & Group 4 decoding tables.
  31.  *
  32.  * This code is derived from code by Michael P. Marking.  In
  33.  * particular, the algorithms to generate the null_mode and
  34.  * horiz_mode state tables are his.  See the comments below
  35.  * for more information.
  36.  *
  37.  * BEGIN (from the original source)
  38.  LEGAL
  39.  *    Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
  40.  *    Scottsdale, Arizona 85252-8039. All rights reserved.
  41.  *
  42.  *    License is granted by the copyright holder to distribute and use this
  43.  *    code without payment of royalties or the necessity of notification as
  44.  *    long as this notice (all the text under "LEGAL") is included.
  45.  *
  46.  *    Reference: $Id: mkg3states.c,v 1.6 91/09/27 09:16:20 sam Exp $
  47.  *
  48.  *    This program is offered without any warranty of any kind. It includes
  49.  *    no warranty of merchantability or fitness for any purpose. Testing and
  50.  *    suitability for any use are the sole responsibility of the user.
  51.  *
  52.  INFORMATION
  53.  *    Although there is no support offered with this program, the author will
  54.  *    endeavor to correct errors. Updates will also be made available from
  55.  *    time to time.
  56.  *
  57.  *    Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
  58.  *    85252-8039 USA. Replies are not guaranteed to be swift. Beginning
  59.  *    July 1990, e-mail may be sent to uunet!ipel!marking.
  60.  *
  61.  *    Also beginning in July 1990, this code will be archived at the
  62.  *    ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
  63.  *    for 300/1200/2400 is (602)274-0462. When logging in, specify user
  64.  *    "public", system "bbs", and password "public".
  65.  *
  66.  *    This code is also available from the C Users Group in volume 317.
  67.  *
  68.  * END (from the original source)
  69.  */
  70. #include <stdio.h>
  71. #include "prototypes.h"
  72.  
  73. #ifndef TRUE
  74. #define    TRUE    1
  75. #define    FALSE    0
  76. #endif
  77.  
  78. #define WHITE    0
  79. #define BLACK    1
  80.  
  81. /*
  82.  * G3 2D and G4 decoding modes.  Note that
  83.  * the vertical modes are ordered so that
  84.  * (mode - MODE_VERT_V0) gives the vertical
  85.  * adjustment for the b1 parameter.
  86.  */
  87. #define MODE_NULL    0
  88. #define MODE_PASS    1
  89. #define MODE_HORIZ    2
  90. #define MODE_VERT_VL3    3
  91. #define MODE_VERT_VL2    4
  92. #define MODE_VERT_VL1    5
  93. #define MODE_VERT_V0    6
  94. #define MODE_VERT_VR1    7
  95. #define MODE_VERT_VR2    8
  96. #define MODE_VERT_VR3    9
  97. #define MODE_UNCOMP    10
  98. #define MODE_ERROR    11
  99. #define MODE_ERROR_1    12
  100.  
  101. unsigned long
  102. DECLARE1(append_0, unsigned long, prefix)
  103. {
  104.     return (prefix + (1L<<16));
  105. }
  106.  
  107. unsigned long
  108. DECLARE1(append_1, unsigned long, prefix)
  109. {
  110.     static unsigned short prefix_bit[16] = {
  111.     0x8000, 0x4000, 0x2000, 0x1000,
  112.     0x0800, 0x0400, 0x0200, 0x0100,
  113.     0x0080, 0x0040, 0x0020, 0x0010,
  114.     0x0008, 0x0004, 0x0002, 0x0001
  115.     };
  116.     unsigned char len = (prefix >> 16) & 0xf;
  117.     return (append_0(prefix) + prefix_bit[len]);
  118. }
  119.  
  120. #define    G3CODES
  121. #include "t4.h"
  122.  
  123. short
  124. DECLARE3(search_table, unsigned long, prefix, tableentry*, tab, int, n)
  125. {
  126.     unsigned short len = (prefix >> 16) & 0xf;
  127.     unsigned short code = (prefix & 0xffff) >> (16 - len);
  128.  
  129.     while (n-- > 0) {
  130.     if (tab->length == len && tab->code == code)
  131.         return ((short) tab->runlen);
  132.     tab++;
  133.     }
  134.     return (G3CODE_INCOMP);
  135. }
  136.  
  137. #define    NCODES(a)    (sizeof (a) / sizeof (a[0]))
  138. short
  139. DECLARE1(white_run_length, unsigned long, prefix)
  140. {
  141.     return (search_table(prefix, TIFFFaxWhiteCodes, NCODES(TIFFFaxWhiteCodes)));
  142. }
  143.  
  144. short
  145. DECLARE1(black_run_length, unsigned long, prefix)
  146. {
  147.     return (search_table(prefix, TIFFFaxBlackCodes, NCODES(TIFFFaxBlackCodes)));
  148. }
  149. #undef NCODES
  150.  
  151. #define MAX_NULLPREFIX    200    /* max # of null-mode prefixes */
  152. typedef    unsigned char NullModeTable[MAX_NULLPREFIX][256];
  153. #define MAX_HORIZPREFIX    250    /* max # of incomplete 1-D prefixes */
  154. typedef    unsigned char HorizModeTable[MAX_HORIZPREFIX][256];
  155.  
  156.   /* the bit string corresponding to this row of the decoding table */
  157. long    null_mode_prefix[MAX_NULLPREFIX];
  158. NullModeTable null_mode;        /* MODE_*, indexed by bit and byte */
  159. NullModeTable null_mode_next_state;    /* next row of decoding tables to use */
  160.   /* number of prefixes or rows in the G4 decoding tables */
  161. short    null_mode_prefix_count = 0;
  162.  
  163. /*
  164.  * 2D uncompressed mode codes.  Note
  165.  * that two groups of codes are arranged
  166.  * so that the decoder can caluclate the
  167.  * length of the run by subtracting the
  168.  * code from a known base value.
  169.  */
  170. #define    UNCOMP_INCOMP    0
  171. /* runs of [0]*1 */
  172. #define    UNCOMP_RUN0    1
  173. #define    UNCOMP_RUN1    2
  174. #define    UNCOMP_RUN2    3
  175. #define    UNCOMP_RUN3    4
  176. #define    UNCOMP_RUN4    5
  177. #define    UNCOMP_RUN5    6
  178. #define    UNCOMP_RUN6    7
  179. /* runs of [0]* w/ terminating color */
  180. #define    UNCOMP_TRUN0    8
  181. #define    UNCOMP_TRUN1    9
  182. #define    UNCOMP_TRUN2    10
  183. #define    UNCOMP_TRUN3    11
  184. #define    UNCOMP_TRUN4    12
  185. /* special code for unexpected EOF */
  186. #define    UNCOMP_EOF    13
  187. /* invalid code encountered */
  188. #define    UNCOMP_INVALID    14
  189.  
  190. long    uncomp_mode_prefix[MAX_NULLPREFIX];
  191. NullModeTable uncomp_mode;
  192. NullModeTable uncomp_mode_next_state;
  193. short    uncomp_mode_prefix_count = 0;
  194.  
  195. /*
  196.  * Decoding action values for horiz_mode.
  197.  */
  198. #define ACT_INCOMP    0        /* incompletely decoded code */
  199. #define ACT_INVALID    1        /* invalide code */
  200. #define    ACT_WRUNT    2        /* terminating white run code */
  201. #define    ACT_WRUN    65        /* non-terminating white run code */
  202. #define    ACT_BRUNT    106        /* terminating black run code */
  203. #define    ACT_BRUN    169        /* non-terminating black run code */
  204. #define ACT_EOL        210        /* end-of-line code */
  205. HorizModeTable horiz_mode;
  206.  
  207. short
  208. DECLARE1(horiz_mode_code_black, short, runlen)
  209. {
  210.     return (runlen < 64 ? runlen + ACT_BRUNT : (runlen / 64) + ACT_BRUN);
  211. }
  212.  
  213. short
  214. DECLARE1(horiz_mode_code_white, short, runlen)
  215. {
  216.     return (runlen < 64 ? runlen + ACT_WRUNT : (runlen / 64) + ACT_WRUN);
  217. }
  218.  
  219. /*
  220.  * If the corresponding horiz_mode entry is ACT_INCOMP
  221.  * this entry is a row number for decoding the next byte;
  222.  * otherwise, it is the bit number with which to continue
  223.  * decoding the next codeword.
  224.  */
  225. HorizModeTable horiz_mode_next_state;
  226.         /* prefixes corresponding to the rows of the decoding table */
  227. long    horiz_mode_prefix[MAX_HORIZPREFIX];
  228.         /* color of next run, BLACK or WHITE */
  229. char    horiz_mode_color[MAX_HORIZPREFIX];
  230. short    horiz_mode_prefix_count = 0;
  231.  
  232. static    unsigned char bit_mask[8] =
  233.     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  234.  
  235. #if USE_PROTOTYPES
  236. void    build_null_mode_tables(void);
  237. short    find_horiz_mode_prefix(long, char);
  238. short    find_null_mode_prefix(long);
  239. short    null_mode_type(long);
  240. void    build_horiz_mode_tables(void);
  241. short    horiz_mode_code_black(short);
  242. short    horiz_mode_code_white(short);
  243. void    build_uncomp_mode_tables(void);
  244. void    write_tables(FILE*);
  245. #else
  246. void    build_null_mode_tables();
  247. short    find_horiz_mode_prefix();
  248. short    find_null_mode_prefix();
  249. short    null_mode_type();
  250. void    build_horiz_mode_tables();
  251. short    horiz_mode_code_black();
  252. short    horiz_mode_code_white();
  253. void    build_uncomp_mode_tables();
  254. void    write_tables();
  255. #endif
  256.  
  257. int    verbose = FALSE;
  258.  
  259. void
  260. DECLARE2(main, int, argc, char**, argv)
  261. {
  262.     if (argc > 1 && strcmp(argv[1], "-v") == 0)
  263.     verbose = TRUE;
  264.     build_null_mode_tables();        /* null mode decoding tables */
  265.     if (verbose) {
  266.     fprintf(stderr, "%hd null mode prefixes defined\n",
  267.         null_mode_prefix_count);
  268.     fprintf(stderr, "building uncompressed mode scripts...\n");
  269.     }
  270.     build_uncomp_mode_tables();        /* uncompressed mode decoding tables */
  271.     if (verbose) {
  272.     fprintf(stderr, "%hd uncompressed mode prefixes defined\n",
  273.         uncomp_mode_prefix_count);
  274.     fprintf(stderr, "building 1D scripts...\n");
  275.     }
  276.     build_horiz_mode_tables();        /* 1D decoding tables */
  277.     if (verbose)
  278.     fprintf(stderr, "%hd incomplete prefixes defined\n",
  279.         horiz_mode_prefix_count);
  280.     write_tables(stdout);
  281.     exit(0);
  282. }
  283.  
  284. void
  285. DECLARE3(write_null_mode_table, FILE*, fd, NullModeTable, table, char*, name)
  286. {
  287.     int i, j;
  288.     char* outersep;
  289.     char* sep;
  290.  
  291.     fprintf(fd, "u_char\t%s[%hd][256] = {", name, null_mode_prefix_count);
  292.     outersep = "";
  293.     for (i = 0; i < null_mode_prefix_count; i++) {
  294.     fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
  295.     sep = "    ";
  296.     for (j = 0; j < 256; j++) {
  297.         fprintf(fd, "%s%2hd", sep, (short) table[i][j]);
  298.         if (((j+1) % 16) == 0) {
  299.         fprintf(fd, ", /* %3d-%3d */\n", j-15, j);
  300.         sep = "    ";
  301.         } else
  302.         sep = ",";
  303.     }
  304.     fprintf(fd, "}");
  305.     outersep = ",";
  306.     }
  307.     fprintf(fd, "\n};\n");
  308. }
  309.  
  310. void
  311. DECLARE3(write_horiz_mode_table, FILE*, fd, HorizModeTable, table, char*, name)
  312. {
  313.     int i, j;
  314.     char* outersep;
  315.     char* sep;
  316.  
  317.     fprintf(fd, "u_char\t%s[%hd][256] = {", name, horiz_mode_prefix_count);
  318.     outersep = "";
  319.     for (i = 0; i < horiz_mode_prefix_count; i++) {
  320.     fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
  321.     sep = "    ";
  322.     for (j = 0; j < 256; j++) {
  323.         fprintf(fd, "%s%3hd", sep, (short) table[i][j]);
  324.         if (((j+1) % 14) == 0) {
  325.         fprintf(fd, ", /* %3d-%3d */\n", j-13, j);
  326.         sep = "    ";
  327.         } else
  328.         sep = ",";
  329.     }
  330.     fprintf(fd, "\n}");
  331.     outersep = ",";
  332.     }
  333.     fprintf(fd, "\n};\n");
  334. }
  335.  
  336. void
  337. write_define(fd, name, value, comment)
  338.     FILE *fd;
  339.     char *name;
  340.     int value;
  341.     char *comment;
  342. {
  343.     fprintf(fd, "#define\t%s\t%d", name, value);
  344.     if (comment)
  345.     fprintf(fd, "\t/* %s */", comment);
  346.     fprintf(fd, "\n");
  347. }
  348.  
  349. void
  350. write_preamble(fd)
  351.     FILE *fd;
  352. {
  353.     fprintf(fd, "%s\n",
  354. "/* DO NOT EDIT THIS FILE, IT WAS AUTOMATICALLY CREATED BY mkg3state */");
  355.     write_define(fd, "ACT_INCOMP", ACT_INCOMP, "incompletely decoded code");
  356.     write_define(fd, "ACT_INVALID", ACT_INVALID, "invalide code");
  357.     write_define(fd, "ACT_WRUNT", ACT_WRUNT, "terminating white run code");
  358.     write_define(fd, "ACT_WRUN", ACT_WRUN, "non-terminating white run code");
  359.     write_define(fd, "ACT_BRUNT", ACT_BRUNT, "terminating black run code");
  360.     write_define(fd, "ACT_BRUN", ACT_BRUN, "non-terminating black run code");
  361.     write_define(fd, "ACT_EOL", ACT_EOL, "end-of-line code");
  362.     fprintf(fd, "\n");
  363.     fprintf(fd, "/* modes that the decoder can be in */\n");
  364.     write_define(fd, "MODE_NULL", MODE_NULL, NULL);
  365.     write_define(fd, "MODE_PASS", MODE_PASS, NULL);
  366.     write_define(fd, "MODE_HORIZ", MODE_HORIZ, NULL);
  367.     write_define(fd, "MODE_VERT_V0", MODE_VERT_V0, NULL);
  368.     write_define(fd, "MODE_VERT_VR1", MODE_VERT_VR1, NULL);
  369.     write_define(fd, "MODE_VERT_VR2", MODE_VERT_VR2, NULL);
  370.     write_define(fd, "MODE_VERT_VR3", MODE_VERT_VR3, NULL);
  371.     write_define(fd, "MODE_VERT_VL1", MODE_VERT_VL1, NULL);
  372.     write_define(fd, "MODE_VERT_VL2", MODE_VERT_VL2, NULL);
  373.     write_define(fd, "MODE_VERT_VL3", MODE_VERT_VL3, NULL);
  374.     write_define(fd, "MODE_UNCOMP", MODE_UNCOMP, NULL);
  375.     write_define(fd, "MODE_ERROR", MODE_ERROR, NULL);
  376.     write_define(fd, "MODE_ERROR_1", MODE_ERROR_1, NULL);
  377.     fprintf(fd, "\n");
  378.     fprintf(fd, "#define\tRUNLENGTH(ix)    (TIFFFaxWhiteCodes[ix].runlen)\n");
  379.     fprintf(fd, "\n");
  380.     write_define(fd, "UNCOMP_INCOMP", UNCOMP_INCOMP, NULL);
  381.     fprintf(fd, "/* runs of [0]*1 */\n");
  382.     write_define(fd, "UNCOMP_RUN0", UNCOMP_RUN0, NULL);
  383.     write_define(fd, "UNCOMP_RUN1", UNCOMP_RUN1, NULL);
  384.     write_define(fd, "UNCOMP_RUN2", UNCOMP_RUN2, NULL);
  385.     write_define(fd, "UNCOMP_RUN3", UNCOMP_RUN3, NULL);
  386.     write_define(fd, "UNCOMP_RUN4", UNCOMP_RUN4, NULL);
  387.     write_define(fd, "UNCOMP_RUN5", UNCOMP_RUN5, NULL);
  388.     write_define(fd, "UNCOMP_RUN6", UNCOMP_RUN6, NULL);
  389.     fprintf(fd, "/* runs of [0]* w/ terminating color */\n");
  390.     write_define(fd, "UNCOMP_TRUN0", UNCOMP_TRUN0, NULL);
  391.     write_define(fd, "UNCOMP_TRUN1", UNCOMP_TRUN1, NULL);
  392.     write_define(fd, "UNCOMP_TRUN2", UNCOMP_TRUN2, NULL);
  393.     write_define(fd, "UNCOMP_TRUN3", UNCOMP_TRUN3, NULL);
  394.     write_define(fd, "UNCOMP_TRUN4", UNCOMP_TRUN4, NULL);
  395.     fprintf(fd, "/* special code for unexpected EOF */\n");
  396.     write_define(fd, "UNCOMP_EOF", UNCOMP_EOF, NULL);
  397.     fprintf(fd, "/* invalid code encountered */\n");
  398.     write_define(fd, "UNCOMP_INVALID", UNCOMP_INVALID, NULL);
  399.     fprintf(fd, "/* codes >= terminate uncompress mode */\n");
  400.     fprintf(fd, "#define\tUNCOMP_EXIT    UNCOMP_TRUN0\n");
  401.     fprintf(fd, "\n");
  402. }
  403.  
  404. void
  405. write_tables(fd)
  406.     FILE* fd;
  407. {
  408.     write_preamble(fd);
  409.     write_null_mode_table(fd, null_mode, "TIFFFax2DMode");
  410.     write_null_mode_table(fd, null_mode_next_state, "TIFFFax2DNextState");
  411.     write_null_mode_table(fd, uncomp_mode, "TIFFFaxUncompAction");
  412.     write_null_mode_table(fd, uncomp_mode_next_state, "TIFFFaxUncompNextState");
  413.     write_horiz_mode_table(fd, horiz_mode, "TIFFFax1DAction");
  414.     write_horiz_mode_table(fd, horiz_mode_next_state, "TIFFFax1DNextState");
  415. }
  416.  
  417. short
  418. DECLARE1(find_null_mode_prefix, long, prefix)
  419. {
  420.     short j1;
  421.  
  422.     if (prefix == 0L)
  423.     return (0);
  424.     for (j1 = 8; j1 < null_mode_prefix_count; j1++)
  425.     if (prefix == null_mode_prefix[j1])
  426.         return (j1);
  427.     if (null_mode_prefix_count == MAX_NULLPREFIX) {
  428.     fprintf(stderr, "ERROR: null mode prefix table overflow\n");
  429.     exit(1);
  430.     }
  431.     if (verbose)
  432.     fprintf(stderr, "adding null mode prefix[%hd] 0x%lx\n",
  433.         null_mode_prefix_count, prefix);
  434.     null_mode_prefix[null_mode_prefix_count++] = prefix;
  435.     return (null_mode_prefix_count-1);
  436. }
  437.  
  438. short
  439. DECLARE2(find_horiz_mode_prefix, long, prefix, char, color)
  440. {
  441.     short j1;
  442.  
  443.     for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
  444.     if (prefix == horiz_mode_prefix[j1] && horiz_mode_color[j1] == color)
  445.         return (j1);
  446.     /*
  447.      * It wasn't found, so add it to the tables, but first, is there room?
  448.      */
  449.     if (horiz_mode_prefix_count == MAX_HORIZPREFIX) {
  450.     fprintf(stderr, "ERROR: 1D prefix table overflow\n");
  451.     exit(1);
  452.     }
  453.     /* OK, there's room... */
  454.     if (verbose)
  455.     fprintf(stderr, "\nhoriz mode prefix %hd, color %c = 0x%lx ",
  456.         horiz_mode_prefix_count, "WB"[color], prefix);
  457.     horiz_mode_prefix[horiz_mode_prefix_count] = prefix;
  458.     horiz_mode_color[horiz_mode_prefix_count] = color;
  459.     horiz_mode_prefix_count++;
  460.     return (horiz_mode_prefix_count - 1);
  461. }
  462.  
  463. short
  464. DECLARE1(find_uncomp_mode_prefix, long, prefix)
  465. {
  466.     short j1;
  467.  
  468.     if (prefix == 0L)
  469.     return (0);
  470.     for (j1 = 8; j1 < uncomp_mode_prefix_count; j1++)
  471.     if (prefix == uncomp_mode_prefix[j1])
  472.         return (j1);
  473.     if (uncomp_mode_prefix_count == MAX_NULLPREFIX) {
  474.     fprintf(stderr, "ERROR: uncomp mode prefix table overflow\n");
  475.     exit(1);
  476.     }
  477.     if (verbose)
  478.     fprintf(stderr, "adding uncomp mode prefix[%hd] 0x%lx\n",
  479.         uncomp_mode_prefix_count, prefix);
  480.     uncomp_mode_prefix[uncomp_mode_prefix_count++] = prefix;
  481.     return (uncomp_mode_prefix_count-1);
  482. }
  483.  
  484. short
  485. DECLARE1(null_mode_type, long, prefix)
  486. {
  487.     switch (prefix) {
  488.     case 0x18000L: return (MODE_VERT_V0);    /* 1 */
  489.     case 0x36000L: return (MODE_VERT_VR1);    /* 011 */
  490.     case 0x34000L: return (MODE_VERT_VL1);    /* 010 */
  491.     case 0x32000L: return (MODE_HORIZ);        /* 001 */
  492.     case 0x41000L: return (MODE_PASS);        /* 0001 */
  493.     case 0x60C00L: return (MODE_VERT_VR2);    /* 0000 11 */
  494.     case 0x60800L: return (MODE_VERT_VL2);    /* 0000 10 */
  495.     case 0x70600L: return (MODE_VERT_VR3);    /* 0000 011 */
  496.     case 0x70400L: return (MODE_VERT_VL3);    /* 0000 010 */
  497.     case 0x80200L: return (MODE_ERROR);        /* 0000 0010 */
  498.     case 0x90300L: return (MODE_ERROR);        /* 0000 0011 0 */
  499.     case 0xA0380L: return (MODE_ERROR);        /* 0000 0011 10 */
  500.     case 0xA03C0L: return (MODE_UNCOMP);    /* 0000 0011 11 */
  501.     /*
  502.      * Under the assumption that there are no
  503.      * errors in the file, then this bit string
  504.      * can only be the beginning of an EOL code.
  505.      */
  506.     case 0x70000L: return (MODE_ERROR_1);    /* 0000 000 */
  507.     }
  508.     return (-1);
  509. }
  510.  
  511. short
  512. DECLARE1(uncomp_mode_type, long, prefix)
  513. {
  514.     short code;
  515.     short len;
  516.     switch (prefix) {
  517.     case 0x18000L: return (UNCOMP_RUN1);    /* 1 */
  518.     case 0x24000L: return (UNCOMP_RUN2);    /* 01 */
  519.     case 0x32000L: return (UNCOMP_RUN3);    /* 001 */
  520.     case 0x41000L: return (UNCOMP_RUN4);    /* 0001 */
  521.     case 0x50800L: return (UNCOMP_RUN5);    /* 0000 1 */
  522.     case 0x60400L: return (UNCOMP_RUN6);    /* 0000 01 */
  523.     case 0x70200L: return (UNCOMP_TRUN0);    /* 0000 001 */
  524.     case 0x80100L: return (UNCOMP_TRUN1);    /* 0000 0001 */
  525.     case 0x90080L: return (UNCOMP_TRUN2);    /* 0000 0000 1 */
  526.     case 0xA0040L: return (UNCOMP_TRUN3);    /* 0000 0000 01 */
  527.     case 0xB0020L: return (UNCOMP_TRUN4);    /* 0000 0000 001 */
  528.     }
  529.     code = prefix & 0xffffL;
  530.     len = (prefix >> 16) & 0xf;
  531.     return ((code || len > 10) ? UNCOMP_INVALID : -1);
  532. }
  533.  
  534. #define    BASESTATE(b)    ((unsigned char) ((b) & 0x7))
  535.  
  536. void
  537. build_null_mode_tables()
  538. {
  539.     short prefix;
  540.  
  541.     /*
  542.      * Note: the first eight entries correspond to
  543.      * a null prefix and starting bit numbers 0-7.
  544.      */
  545.     null_mode_prefix_count = 8;
  546.     for (prefix = 0; prefix < null_mode_prefix_count; prefix++) {
  547.     short byte;
  548.     for (byte = 0; byte < 256; byte++) {
  549.         short firstbit;
  550.         short bit;
  551.         long curprefix;
  552.         char found_code = FALSE;
  553.  
  554.         if (prefix < 8) {
  555.         curprefix = 0L;
  556.         firstbit = prefix;
  557.         } else {
  558.         curprefix = null_mode_prefix[prefix];
  559.         firstbit = 0;
  560.         }
  561.         for (bit = firstbit; bit < 8 && !found_code; bit++) {
  562.         short mode;
  563.  
  564.         if (bit_mask[bit] & byte)
  565.             curprefix = append_1(curprefix);
  566.         else
  567.             curprefix = append_0(curprefix);
  568.         switch (mode = null_mode_type(curprefix)) {
  569.         case MODE_PASS:
  570.         case MODE_HORIZ:
  571.         case MODE_VERT_V0:
  572.         case MODE_VERT_VR1:
  573.         case MODE_VERT_VR2:
  574.         case MODE_VERT_VR3:
  575.         case MODE_VERT_VL1:
  576.         case MODE_VERT_VL2:
  577.         case MODE_VERT_VL3:
  578.         case MODE_UNCOMP:
  579.         case MODE_ERROR:
  580.         case MODE_ERROR_1:
  581.             /*
  582.              * NOTE: if the bit number is 8, then the table
  583.              * entry will be zero, which indicates a new byte
  584.              * is to be fetched during the decoding process
  585.              */
  586.             found_code = TRUE;
  587.             null_mode[prefix][byte] = (unsigned char) mode;
  588.             null_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  589.             break;
  590.         }
  591.         }
  592.         if (!found_code) {
  593.         null_mode_next_state[prefix][byte] = (unsigned char)
  594.             find_null_mode_prefix(curprefix);
  595.         /*
  596.          * This indicates to the decoder that
  597.          * no valid code has yet been identified.
  598.          */
  599.         null_mode[prefix][byte] = MODE_NULL;
  600.         }
  601.     }
  602.     }
  603. }
  604.  
  605. void
  606. build_horiz_mode_tables()
  607. {
  608.     unsigned short byte;
  609.     short prefix;
  610.  
  611.     /*
  612.      * The first 8 are for white,
  613.      * the second 8 are for black,
  614.      * beginning with bits 0-7.
  615.      */
  616.     horiz_mode_prefix_count = 16;
  617.     for (prefix = 0; prefix < horiz_mode_prefix_count; prefix++)
  618.     for (byte = 0; byte < 256; byte++) {
  619.         short bits_digested = 0;
  620.         short bit;
  621.         short firstbit;
  622.         char color;
  623.         unsigned long curprefix;
  624.  
  625.         if (prefix < 8) {
  626.         color = WHITE;
  627.         curprefix = 0L;
  628.         firstbit = prefix;
  629.         } else if (prefix < 16) {
  630.         color = BLACK;
  631.         curprefix = 0L;
  632.         firstbit = prefix - 8;
  633.         } else {
  634.         color = horiz_mode_color[prefix];
  635.         curprefix = horiz_mode_prefix[prefix];
  636.         firstbit = 0;
  637.         }
  638.         for (bit = firstbit; bit < 8 && !bits_digested; bit++) {
  639.         if (bit_mask[bit] & byte)
  640.             curprefix = append_1(curprefix);
  641.         else
  642.             curprefix = append_0(curprefix);
  643.         /*
  644.          * The following conversion allows for arbitrary strings of
  645.          * zeroes to precede the end-of-line code 0000 0000 0001.
  646.          * It assumes no errors in the data, and is based on
  647.          * the assumption that the code replaced (12 consecutive
  648.          * zeroes) can only be "legally" encountered before the
  649.          * end-of-line code.  This assumption is valid only for
  650.          * a Group 3 image; the combination will never occur
  651.          * in horizontal mode in a proper Group 4 image.
  652.          */
  653.         if (curprefix == 0xC0000L)
  654.             curprefix = 0xB0000L;
  655.         if (color == WHITE) {
  656.             short runlength = white_run_length(curprefix);
  657.  
  658.             if (runlength == G3CODE_INVALID) {
  659.             horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
  660.             horiz_mode_next_state[prefix][byte] = (unsigned char) bit;
  661.             bits_digested = bit + 1;
  662.             } else if (runlength == G3CODE_EOL) { /* Group 3 only */
  663.             horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
  664.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  665.             bits_digested = bit + 1;
  666.             } else if (runlength != G3CODE_INCOMP) {
  667.             horiz_mode[prefix][byte] = (unsigned char)
  668.                 horiz_mode_code_white(runlength);
  669.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  670.             bits_digested = bit + 1;
  671.             }
  672.         } else {        /* color == BLACK */
  673.             short runlength = black_run_length(curprefix);
  674.  
  675.             if (runlength == G3CODE_INVALID) {
  676.             horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
  677.             horiz_mode_next_state[prefix][byte] = (unsigned char) (bit+8);
  678.             bits_digested = bit + 1;
  679.             } else if (runlength == G3CODE_EOL) { /* Group 3 only */
  680.             horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
  681.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  682.             bits_digested = bit + 1;
  683.             } else if (runlength != G3CODE_INCOMP) {
  684.             horiz_mode[prefix][byte] = (unsigned char)
  685.                 horiz_mode_code_black(runlength);
  686.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  687.             bits_digested = bit + 1;
  688.             }
  689.         }
  690.         }
  691.         if (!bits_digested) {    /* no codewords after examining byte */
  692.         horiz_mode[prefix][byte] = (unsigned char) ACT_INCOMP;
  693.         horiz_mode_next_state[prefix][byte] = (unsigned char)
  694.             find_horiz_mode_prefix(curprefix, color);
  695.         }
  696.     }
  697. }
  698.  
  699. void
  700. build_uncomp_mode_tables()
  701. {
  702.     short prefix;
  703.  
  704.     /*
  705.      * Note: the first eight entries correspond to
  706.      * a null prefix and starting bit numbers 0-7.
  707.      */
  708.     uncomp_mode_prefix_count = 8;
  709.     for (prefix = 0; prefix < uncomp_mode_prefix_count; prefix++) {
  710.     short byte;
  711.     for (byte = 0; byte < 256; byte++) {
  712.         short firstbit;
  713.         short bit;
  714.         long curprefix;
  715.         char found_code = FALSE;
  716.  
  717.         if (prefix < 8) {
  718.         curprefix = 0L;
  719.         firstbit = prefix;
  720.         } else {
  721.         curprefix = uncomp_mode_prefix[prefix];
  722.         firstbit = 0;
  723.         }
  724.         for (bit = firstbit; bit < 8 && !found_code; bit++) {
  725.         short mode;
  726.  
  727.         if (bit_mask[bit] & byte)
  728.             curprefix = append_1(curprefix);
  729.         else
  730.             curprefix = append_0(curprefix);
  731.         mode = uncomp_mode_type(curprefix);
  732.         if (mode != -1) {
  733.             /*
  734.              * NOTE: if the bit number is 8, then the table
  735.              * entry will be zero, which indicates a new byte
  736.              * is to be fetched during the decoding process
  737.              */
  738.             found_code = TRUE;
  739.             uncomp_mode[prefix][byte] = (unsigned char) mode;
  740.             uncomp_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  741.             break;
  742.         }
  743.         }
  744.         if (!found_code) {
  745.         uncomp_mode_next_state[prefix][byte] = (unsigned char)
  746.             find_uncomp_mode_prefix(curprefix);
  747.         /*
  748.          * This indicates to the decoder that
  749.          * no valid code has yet been identified.
  750.          */
  751.         uncomp_mode[prefix][byte] = UNCOMP_INCOMP;
  752.         }
  753.     }
  754.     }
  755. }
  756.